#include "main_head.h"
#include "head/Bird.h"

Bird::Bird(GLint _program) {
	Program = _program;
}

void Bird::set_live(GLint _live) {
	live = _live;
}

void Bird::fall() {
	if (fall_up_time)
	{
		time = 1;
		birdPositions[0].y += 0.0000001f * fall_up_time * fall_up_time;//h = 1/2at^2
		fall_up_time--;
	}
	else
	{
		birdPositions[0].y -= 0.00000001f * fall_time * fall_time;//h = 1/2at^2
		fall_time++;
	}
	angle += 0.003f;
	if (angle >= 1.56)angle = 1.56;

	glm::mat4 model;
	model = glm::translate(model, birdPositions[0]);
	model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, -1.0f));
	GLint viewLoc = glGetUniformLocation(Program, "model");
	glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(model));
}

void Bird::control(GLint up) {
	if (up)up_time = 130, angle = -1.0;
	if (up_time)
	{
		time = 1;
		birdPositions[0].y += 0.0000001f * up_time * up_time;//h = 1/2at^2
		up_time--;
	}
	else
	{
		birdPositions[0].y -= 0.00000001f * time * time;//h = 1/2at^2
		time++;
	}
	angle += 0.003f;
	if (angle >= 1.56)angle = 1.56;

	glm::mat4 model;
	model = glm::translate(model, birdPositions[0]);
	model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, -1.0f));
	GLint viewLoc = glGetUniformLocation(Program, "model");
	glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(model));
}

void Bird::load_image() {
	int width, height;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture); // All upcoming GL_TEXTURE_2D operations now have effect on our texture object
	// Set our texture parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// Set texture wrapping to GL_REPEAT
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// Set texture filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	// Load, create texture and generate mipmaps

	unsigned char* image = SOIL_load_image("picture/Bird.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	glGenerateMipmap(GL_TEXTURE_2D);
	SOIL_free_image_data(image);
	glBindTexture(GL_TEXTURE_2D, 0);
}

void Bird::load_coord() {
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);

	glBindVertexArray(VAO);

	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(position), position, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
	glEnableVertexAttribArray(1);
}

bool Bird::judge(glm::vec3 pipePositions[], int view) {
	if (birdPositions[0].y > 1.0f || birdPositions[0].y < -1.0f)
		return false;

	//std::cout << x << "		" << y << "\n";

	for (int i = 0; i < 6; i++) {
		if ( -0.109 < pipePositions[i].x - view * 0.0005 && pipePositions[i].x - view * 0.0005 < 0.107 ) {
			if (birdPositions[0].y <= -0.1 + pipePositions[i].y || birdPositions[0].y >= 0.1 + pipePositions[i].y) {
				return false;
			}
		}
	}
	return true;
}

bool Bird::get_the_point(glm::vec3 pipePositions[], int view) {
	for (int i = 0; i < 6; i++) {
		if (-0.0001 < pipePositions[i].x - view * 0.0005 && pipePositions[i].x - view * 0.0005 < 0.0001) {
			return true;
		}
	}
	return false;
}

void Bird::draw() {
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texture);
	glUniform1i(glGetUniformLocation(Program, "ourTexture"), 0);
	glBindVertexArray(VAO);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
	glBindVertexArray(0);
}

void Bird::burn() {
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);
}